package com.yz.group1.group_1.controller;

import com.yz.group1.group_1.entity.Schedule;
import com.yz.group1.group_1.service.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin // 支持跨域请求
@RestController
@RequestMapping("/schedule2")
public class ScheduleController {

    private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);

    @Autowired
    private ScheduleService scheduleService;

    // 获取所有排班
    @GetMapping
    public ResponseEntity<Map<String, Object>> scheduleList() {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Schedule> schedules = scheduleService.listAllSchedules();
            if (schedules != null) {
                response.put("success", true);
                response.put("data", schedules);
                return new ResponseEntity<>(response, HttpStatus.OK);
            } else {
                response.put("success", false);
                response.put("message", "查询所有排班信息失败");
                return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            logger.error("查询所有排班信息出错", e);
            response.put("success", false);
            response.put("message", "查询所有排班信息出错");
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 新增：查询个人排班
    @GetMapping("/personal/{doctorId}")
    public ResponseEntity<Map<String, Object>> getPersonalSchedule(@PathVariable Integer doctorId) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Schedule> schedules = scheduleService.getPersonalSchedule(doctorId);
            if (schedules != null) {
                response.put("success", true);
                response.put("data", schedules);
                return new ResponseEntity<>(response, HttpStatus.OK);
            } else {
                response.put("success", false);
                response.put("message", "查询医生ID为 " + doctorId + " 的排班信息失败");
                return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            logger.error("查询医生ID为 {} 的排班信息出错", doctorId, e);
            response.put("success", false);
            response.put("message", "查询医生ID为 " + doctorId + " 的排班信息出错");
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 新增：标记排班状态
    @PutMapping("/status/{id}")
    public ResponseEntity<Map<String, Object>> updateScheduleStatus(
            @PathVariable Integer id,
            @RequestParam Integer status) {
        Map<String, Object> response = new HashMap<>();
        try {
            Schedule schedule = new Schedule();
            schedule.setId(id);
            schedule.setStatus(status);
            boolean result = scheduleService.updateById(schedule);
            if (result) {
                response.put("success", true);
                response.put("message", "排班状态更新成功");
                return new ResponseEntity<>(response, HttpStatus.OK);
            } else {
                response.put("success", false);
                response.put("message", "排班状态更新失败");
                return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            logger.error("更新排班ID为 {} 的状态出错", id, e);
            response.put("success", false);
            response.put("message", "更新排班ID为 " + id + " 的状态出错");
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}